﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace WebApp.Management.Product
{
    public partial class orderlist : PageBase2
    {
        private OrderManager m_orderManger = new OrderManager();
        private RoleManager m_RoleManager = new RoleManager();
        protected DataSet ds = new DataSet();
        Guid cid;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }

            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            ViewState["ds"] = m_orderManger.GetAllorders_Search(this.txtOrderNum.Text, this.txtUserName.Text, this.txtTel.Text,this.ckboxRefund.Checked);
            this.lvActivity.DataSource = ViewState["ds"] as DataSet;
            this.lvActivity.DataBind();
        }

        protected void lvActivity_ItemCommand(object sender, ListViewCommandEventArgs e)
        {  
            if (e.CommandName.Equals("OnDelete"))
            {
                cid = new Guid (e.CommandArgument.ToString());
               string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                string cmdStr = ("select * from OrderDetail where OrderId='" + cid + "'");
                SqlCommand com = new SqlCommand(cmdStr, con);
                SqlDataAdapter da = new SqlDataAdapter(com);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.HasRows == true)
                {
                    Response.Write("<script>alert('请先删除此订单的详细信息!')</script>");
                       return;
                }
                else
                {
                   OrderManager.DeleteSaleProducts(new Guid ((e.CommandArgument.ToString())));

                   BindGrid();
                }
            }
        }
        protected void lvActivity_PagePropertiesChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}
